package com.xzlf.controller;

import com.xzlf.domain.NovelChapter;
import com.xzlf.domain.NovelInfo;
import com.xzlf.domain.WebSite;
import com.xzlf.service.NovelChapterService;
import com.xzlf.service.NovelInfoService;
import com.xzlf.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private WebSiteService webSiteService;

    @Autowired
    private NovelInfoService novelInfoService;

    @Autowired
    private NovelChapterService novelChapterService;

    /**
     * 进入管理页面
     * @param model
     * @return
     */
    @GetMapping("/")
    public String admin(Model model){
        List<WebSite> list = webSiteService.list();
        model.addAttribute("list" , list);
        return "admin/admin";
    }

    /**
     * 进入添加网站页面
     *
     * @return
     */
    @GetMapping("/addWeb")
    public String addWeb(){
        return "admin/addWeb";
    }

    /**
     * 保存网站
     * @param webSite
     * @param model
     * @return
     */
    @PostMapping("/saveWeb")
    public String saveWeb(WebSite webSite, Model model){
        boolean save = webSiteService.save(webSite);
        return save? admin(model) : "addFail";
    }

    /**
     * 进入添加小说页面
     *
     * @return
     */
    @GetMapping("/addNovel")
    public String addNovel(@RequestParam(required = false) int pid, Model model){
        if (pid != 0){
            model.addAttribute("pid", pid);
        }
        return "admin/addNovel";
    }

    /**
     * 保存小说章节信息
     * @param url
     * @param popNum
     * @param novelInfo
     * @param model
     * @return
     */
    @PostMapping("/saveNovel")
    public String saveNovel(String url, int popNum, NovelInfo novelInfo, Model model){
        boolean save = novelInfoService.saveNovel(url,popNum, novelInfo);
        return save? "addOk" : "addFail";
    }

    /**
     * 查看小说列表
     * @param model
     * @return
     */
    @GetMapping("/findNovel")
    public String findNovel(@RequestParam(defaultValue = "0") int pid, Model model){
        List<NovelInfo> list = novelInfoService.findNovel(pid);
        model.addAttribute("list", list);
        return "novelList";
    }


    @RequestMapping("/findByPage")
    public String findByPage( int pid,
                            @RequestParam(defaultValue = "1") int current,
                            @RequestParam(defaultValue = "50")int pageSize, Model model){
        List<NovelChapter> list = novelChapterService.findByPage(pid, current, pageSize);
        model.addAttribute("list", list);
        model.addAttribute("pid", pid);
        model.addAttribute("current", current);
        model.addAttribute("pageSize", pageSize);
        return "client/novelChapterList";
    }
}
